/**
 * @file test.as

 修改说明：（在原来版本功能上修改）
 1、模板显示框可以设置大小，打印模板能在模板显示框里自适应全屏展示；
 2、背景与按键UI可以替换，
 3、副屏是否需要展示，可以自己设置启动或不启动；
 4、输出2个版本：横版与竖版  （横版UI就拿原版本修改就好）
 ps：其他功能与原来版本一样；
 模板确认按键就是之前的完成按键；



config:
横版的模板目录:
bin/yonghe/print
竖版的模板目录:
bin/yonghe/prints

bin/yonghe/config.json:
"quitTime":0,//无人操作回到首页的时间,默认值为60,(单位：秒)
quitTime>0,自动退出,无副屏
quitTime<=0,
hasWindow2:有无副屏



 *  
 * @author db0@qq.com
 * @version 1.0.1
 * @date 2018-10-26
 */
package yonghe
{
	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.utils.clearTimeout;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;

	[SWF(height="640",width="360",frameRate="12",backgroundColor=0xeeeeee)]
		[SWF(width="1920",height="1080",frameRate="12",backgroundColor=0xeeeeee)]
			CONFIG::ISVERT{
				[SWF(width="1080",height="1920",frameRate="12",backgroundColor=0xeeeeee)]
			}
	[SWF(width="640",height="360",frameRate="12",backgroundColor=0xeeeeee)]
	public class test extends Sprite
	{
		private static var _stageW:int;
		private static var _stageH:int;
		private static var _main:test;

		public static function get main():test
		{
			return _main;
		}
		public static function get stageW():int
		{
			return _stageW;
		}
		public static function get stageH():int
		{
			return _stageH;
		}
		public function test()
		{
			_main = this;

			if(stage)init(null);
			else addEventListener(Event.ADDED_TO_STAGE,init);
		}

		private static var _startTime:uint;
		public static function get startTime():uint
		{
			return _startTime;
		}
		public static function get now():uint
		{
			return getTimer();
		}
		public static function get passedTime():uint
		{
			return now - startTime;
		}
		public static function get isVert():Boolean
		{
			CONFIG::ISVERT{return true;}
			return false;
		}

		private function init(e:Event):void
		{
			addChild(new Authorize("47423847"));
			removeEventListener(Event.ADDED_TO_STAGE,init);
			//addChild(new logs);
			logs.adds(Datas.quitTime);
			_stageW = stage.stageWidth;
			_stageH = stage.stageHeight;

			SwfLoader.SwfLoad(Assets.bgUrl,loaded);

			var full:Boolean = true;
			CONFIG::debugging{ full=false; }
			//stage.align = StageAlign.topLeft;
			if(full){
				stage.scaleMode = StageScaleMode.EXACT_FIT;
				stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
				//stage.nativeWindow.alwaysInFront = true;
				//stage.displayState = StageDisplayState.NORMAL;
			}
			addChild(new Toast(0x0,0xf2f2f2,_stageW,_stageH));
		}

		public static function key_events(e:KeyboardEvent):void
		{
			switch(e.keyCode){
				case Keyboard.BACK:
				case Keyboard.ESCAPE:
				case Keyboard.F4:
				case Keyboard.TAB:
					e.preventDefault();
					logs.adds("ESCAPE");
					Toast.show("确认退出");
					quit();
					break;
			}
		}
		private static function quit():void
		{
			NativeApplication.nativeApplication.exit();
		}

		private var _bg:Bitmap;
		public function get bgBmpd():BitmapData
		{
			if(_bg==null)
			{
				if(isVert){
					_bg = new Bitmap(new BitmapData(1080,1920));
				}else{
					_bg = new Bitmap(new BitmapData(1920,1800));
				}
			}
			return _bg.bitmapData;
		}
		private function loaded(e:Event):void
		{
			if(e && e.type == Event.COMPLETE)
			{
				_bg = e.target.content as Bitmap;
			}else{
				logs.adds(e);
			}
			//Datas.init();

			stage.addEventListener(MouseEvent.MOUSE_UP,resetClock);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,resetClock);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,resetClock);
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, key_events);

			_startTime = now;
			SignPage.show();
			if(Datas.hasWindow2 && !Datas.autoQuit)
				Window2.show();
		}

		private var timeoutId:uint;
		private function resetClock(e:MouseEvent):void
		{
			clearTimeout(timeoutId);
			if(Datas.hasWindow2==false && Datas.autoQuit){
				timeoutId = setTimeout(quit,Datas.quitTime*1000);
			}
		}
		public static function show(sp:Sprite):Sprite
		{
			main.addChild(sp);
			sp.visible = true;
			return sp;
		}
	}
}

